import Vue from 'vue'
import store from './store'
import App from './App'
import tabBar from './components/tabBar.vue'
import fui from '@/uni_modules/ddiot-ui/js_sdk/index.js'
import diandi from './config.js'
import uView from '@/uni_modules/uview-ui'
import * as api from '@/api/api.js'
Vue.use(uView)

import {
	router,
	RouterMount
} from './router.js' //路径换成自己的
Vue.use(router)


// #ifdef H5
window.QQmap = null;
// #endif
// #ifndef MP-TOUTIAO
//网络监听
setTimeout(() => {
	uni.onNetworkStatusChange(function(res) {
		//console.log(res.networkType);
		store.commit("networkChange", {
			isConnected: res.isConnected
		})
	});
}, 100)
// #endif
Vue.prototype.fui = fui
Vue.prototype.$eventHub = Vue.prototype.$eventHub || new Vue()
Vue.prototype.$store = store
Vue.component('tab-bar', tabBar)
Vue.prototype.$store = store
Vue.config.productionTip = false
Vue.prototype.$api = api
App.mpType = 'app'



Vue.use(fui, {
	websoketConf: null,
	shareConf:{
		title: '相随相亲',
		path: '/pages/index/index', // 全局分享的路径，比如 首页
		imageUrl: 'https://www.xiangsui168.com/uploads/20240606/72f836e5c615eb89677df3fd4127458c.png',
	},
	// 请求相关的配置
	http: {
		successCode: 1,
		apiConfig: {
			'bloc_id': diandi.bloc_id,
			'store_id': diandi.store_id,
			'app_secret': '2',
			'app_id': '1',
			'baseUrl': diandi.baseUrl,
			'siteUrl': diandi.siteUrl,
			'uploadImgUrl': diandi.baseUrl + '/upload/images',
			'imgBaseUrl': diandi.baseUrl + '/attachment/',
			// 代理名称
			'proxyName': diandi.baseUrl,
			'tokenApi': '/wechat/basics/signup',
			'refreshApi': '/user/refresh',
			// 备用域名配置,至少配置一个,这边会自动设置baseUrl，至少传入一个域名，H5本地调试会自己代理到proxyName
			'domain': diandi.baseUrl,
			'iot': {
				'app_secret': '',
				'app_id': '',
				'baseUrl': null,
			},
			// 签名算法
			// signFunc: (res, app_id, app_secret) => {
			// 	console.log('签名算法', res, app_id, app_secret)
			// 	return Object.assign({
			// 		sign: 666
			// 	}, res)
			// },
			// 动态头部参数
			headerFunc: (config) => {
				console.log('头部处理', uni.getStorageSync('access_token'))
				return {
					'token': uni.getStorageSync('access_token') || ''
				}
			},
			getToken: (config) => {
				let accessToken = uni.getStorageSync('access_token');
				return accessToken;
			},
			getRefToken: (config) => {
				let refresh_token = uni.getStorageSync('refresh_token');
				return refresh_token;
			},
			refTokenCallBack: (res) => {
				console.log('refTokenCallBack', res)
				uni.setStorageSync('access_token', res.data.token);
			}
		},
		responseSuccessCallBack: (res, catchObj, query) => {
			console.log('请求触发1', res, catchObj, query);
			if (res && (res.code === 401 || (res.data && res.data.code === 401))) {
				console.log('请求触发1--403');
				uni.removeStorageSync('access_token');
				// #ifdef H5
				console.log('请求触发1--403 -- h5');
				let userAgent = navigator.userAgent;
				const ua = window.navigator.userAgent.toLowerCase();
				let _isWeixin = ua.match(/micromessenger/i) == 'micromessenger'
				console.log('请求触发1--403 -- _isWeixin');
				console.log('_isWeixin2', _isWeixin);
				if (_isWeixin) { // 公众号
					console.log('当前为公众号环境')
					fui.href('/wechatlogin')
				} else { //h5
					console.log('当前为H5环境')
					fui.href('/login')
				}

				// #endif

			}else if(res && (res.code === 406 || (res.data && res.data.code === 406))){
				fui.href('/')
			}
		}
	}
})

const app = new Vue({
	store,
	...App
})

//v1.3.5起 H5端 你应该去除原有的app.$mount();使用路由自带的渲染方式
// #ifdef H5
RouterMount(app, router, '#app')
// #endif

// #ifndef H5
app.$mount(); //为了兼容小程序及app端必须这样写才有效果
// #endif